package com.ktjy.web.dao.impl;


import com.ktjy.web.dao.BaseDao;
import com.ktjy.web.dao.RoleDao;
import com.ktjy.web.dao.UserDao;
import com.ktjy.web.entity.Role;
import com.ktjy.web.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class RoleDaoImpl extends BaseDao implements RoleDao {
    @Override
    public List<Role> selRoleAll() {
        List<Role> list=null;
        SqlSession sqlSession= MyBatisUtil.createSqlSession();
        try {
            list=sqlSession.getMapper(RoleDao.class).selRoleAll();
        }
        finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return list;
    }

    @Override
    public List<Role> selRoleByNameAll(String roleName) {
        List<Role> list=null;
        SqlSession sqlSession= MyBatisUtil.createSqlSession();
        try {
            list=sqlSession.getMapper(RoleDao.class).selRoleByNameAll(roleName);
        }
        finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return list;
    }

    @Override
    public Role selRoleById(int id) {
        Role role=null;

        SqlSession sqlSession= MyBatisUtil.createSqlSession();
        try {
            role=sqlSession.getMapper(RoleDao.class).selRoleById(id);
        }
        finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }

        return role;
    }

    @Override
    public Role selRoleByName(String roleName) {
        Role role=null;
        SqlSession sqlSession= MyBatisUtil.createSqlSession();
        try {
            role=sqlSession.getMapper(RoleDao.class).selRoleByName(roleName);
        }
        finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return role;
    }

    @Override
    public int addRole(Role role) {
        int count=0;
        SqlSession sqlSession= MyBatisUtil.createSqlSession();
        try {
            count=sqlSession.getMapper(RoleDao.class).addRole(role);
        }
        finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return count;
    }

    @Override
    public int updateRole(Role role) {
        int count=0;
        SqlSession sqlSession= MyBatisUtil.createSqlSession();
        try {
            count=sqlSession.getMapper(RoleDao.class).updateRole(role);
        }
        finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return count;
    }

    @Override
    public int delRole(int id) {
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        Connection conn = this.getConnection();
        int count=0;

        String sql = "UPDATE t_role SET enabled=1 WHERE roleId=?";
        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1,id);

            count= pstmt.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            super.closeAll(rs, pstmt, conn);
        }
        return count;
    }
}

